package com.boctech.yxt.authcenter.service;

import com.boctech.yxt.authcenter.entity.MyUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Component
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    MyRBCAService myRBCAService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MyUserDetails userDetails = myRBCAService.selectUserDetailsByUserName(username);
        List<String> stringAuthorites = myRBCAService.selectAuthoritiesByUserName(username);
        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(String.join(",", stringAuthorites));
        userDetails.setAuthorities(grantedAuthorities);
        return userDetails;
    }
}
